package com.library.admin.listener;

import com.library.admin.AdmLibrary;
import com.library.admin.MainPanel;
import com.library.admin.Menu;
import com.library.admin.form.UpdateOwnAccountForm;
import com.library.admin.manager.BookManagerPanel;
import com.library.admin.manager.RentManagerPanel;
import com.library.admin.manager.SubscriberManagerPanel;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Component;
import com.vaadin.ui.Form;

public class UpdateMainPanelContentListener implements ClickListener {
	private static final long serialVersionUID = -8950854783108514554L;

	public void buttonClick(ClickEvent event) {
		Button clickedButton = (Button) event.getSource();
		AdmLibrary libraryApplication = (AdmLibrary) clickedButton.getApplication();
		
		MainPanel mainPanel = libraryApplication.getMainPanel();
		Menu menu = libraryApplication.getMenu();
		Component componentToAdd = null;
		
		if (clickedButton.getCaption().equals("Modifier vos données")) {
			componentToAdd = new UpdateOwnAccountForm(libraryApplication.getConnectedUser());
		}
		else if (clickedButton.getCaption().equals("Gestion des livres")) {
			componentToAdd = new BookManagerPanel();
		}
		else if (clickedButton.getCaption().equals("Gestion des utilisateurs")) {
			componentToAdd = new SubscriberManagerPanel();
		}
		else if (clickedButton.getCaption().equals("Gestion des locations")) {
			componentToAdd = new RentManagerPanel();
		}
		
		if (mainPanel.getComponentCount() == 0) {
			mainPanel.addComponent(componentToAdd);
		}
		else {
			mainPanel.replaceComponent(mainPanel.getComponent(0), componentToAdd);
		}
		
		//S'il existe des formulaires dans le menu, on les supprime
		for (int i = 0; i < menu.getComponentCount(); i++) {
			Component currentComponent = menu.getComponent(i);
			if (currentComponent instanceof Form) {
				menu.removeComponent(currentComponent);
			}
		}
	}
}
